from elasticsearch import Elasticsearch
import time
import random

def search_data(es):
    res = es.search(index="my_store",doc_type="products")
    print(res)
    print(res['hits']['hits'])


def create_data(es):
    ll = [{'lat': '23.043710', 'lng': '113.518910'}, {'lat': '23.044645', 'lng': '113.522620'},
          {'lat': '23.045316', 'lng': '113.520281'}, {'lat': '23.051141', 'lng': '113.514745'},
          {'lat': '23.046158', 'lng': '113.519713'}, {'lat': '23.046790', 'lng': '113.521816'},
          {'lat': '23.047165', 'lng': '113.525121'}, {'lat': '23.049297', 'lng': '113.518061'},
          {'lat': '23.047777', 'lng': '113.519728'}, {'lat': '23.048587', 'lng': '113.523119'},
          {'lat': '23.048727', 'lng': '113.520952'}, {'lat': '23.047655', 'lng': '113.519695'},
          {'lat': '23.048563', 'lng': '113.521004'}, {'lat': '23.049195', 'lng': '113.517893'},
          {'lat': '23.049373', 'lng': '113.523326'}, {'lat': '23.048490', 'lng': '113.521782'},
          {'lat': '23.049300', 'lng': '113.515495'}, {'lat': '23.050330', 'lng': '113.514422'},
          {'lat': '23.054419', 'lng': '113.514293'}, {'lat': '23.054142', 'lng': '113.515949'},
          {'lat': '23.053412', 'lng': '113.518416'}, {'lat': '23.055504', 'lng': '113.513867'},
          {'lat': '23.053068', 'lng': '113.518877'}, {'lat': '23.051252', 'lng': '113.521161'},
          {'lat': '23.050955', 'lng': '113.526332'}, {'lat': '23.055753', 'lng': '113.521492'},
          {'lat': '23.056839', 'lng': '113.514583'}, {'lat': '23.057056', 'lng': '113.518089'},
          {'lat': '23.054628', 'lng': '113.519912'}, {'lat': '23.053226', 'lng': '113.521745'},
          {'lat': '23.051824', 'lng': '113.523327'}, {'lat': '23.050679', 'lng': '113.522619'},
          {'lat': '23.049040', 'lng': '113.524207'}, {'lat': '23.051015', 'lng': '113.528790'},
          {'lat': '23.053246', 'lng': '113.523812'}, {'lat': '23.053700', 'lng': '113.520679'},
          {'lat': '23.058893', 'lng': '113.517012'}, {'lat': '23.056919', 'lng': '113.520660'},
          {'lat': '23.055319', 'lng': '113.525466'}, {'lat': '23.055438', 'lng': '113.528041'},
          {'lat': '23.054609', 'lng': '113.530123'}, {'lat': '23.050345', 'lng': '113.525724'},
          {'lat': '23.050029', 'lng': '113.525423'}, {'lat': '23.056327', 'lng': '113.532934'},
          {'lat': '23.057373', 'lng': '113.528938'}, {'lat': '23.057602', 'lng': '113.527088'},
          {'lat': '23.058234', 'lng': '113.524255'}, {'lat': '23.063189', 'lng': '113.517088'},
          {'lat': '23.067138', 'lng': '113.513406'}, {'lat': '23.061073', 'lng': '113.510725'},
          {'lat': '23.061054', 'lng': '113.508208'}, {'lat': '23.062258', 'lng': '113.504650'},
          {'lat': '23.066187', 'lng': '113.523851'}, {'lat': '23.064687', 'lng': '113.526426'},
          {'lat': '23.064430', 'lng': '113.530289'}, {'lat': '23.062377', 'lng': '113.530852'},
          {'lat': '23.061390', 'lng': '113.531732'}, {'lat': '23.061923', 'lng': '113.535873'},
          {'lat': '23.070155', 'lng': '113.538019'}, {'lat': '23.071256', 'lng': '113.543942'},
          {'lat': '23.069894', 'lng': '113.542480'}, {'lat': '23.066577', 'lng': '113.542995'},
          {'lat': '23.068255', 'lng': '113.543016'}, {'lat': '23.063794', 'lng': '113.541300'},
          {'lat': '23.077435', 'lng': '113.546020'}, {'lat': '23.075737', 'lng': '113.541385'},
          {'lat': '23.076033', 'lng': '113.538808'}, {'lat': '23.078284', 'lng': '113.544108'},
          {'lat': '23.079292', 'lng': '113.537704'}, {'lat': '23.069289', 'lng': '113.520584'},
          {'lat': '23.072958', 'lng': '113.527644'}, {'lat': '23.070296', 'lng': '113.527493'},
          {'lat': '23.069408', 'lng': '113.527257'}, {'lat': '23.069803', 'lng': '113.514471'},
          {'lat': '23.058984', 'lng': '113.508056'}, {'lat': '23.060287', 'lng': '113.513163'},
          {'lat': '23.068973', 'lng': '113.502863'}, {'lat': '23.064156', 'lng': '113.505107'},
          {'lat': '23.063401', 'lng': '113.501652'}, {'lat': '23.060598', 'lng': '113.527444'},
          {'lat': '23.064112', 'lng': '113.519247'}, {'lat': '23.061733', 'lng': '113.520084'},
          {'lat': '23.060324', 'lng': '113.518829'}, {'lat': '23.075557', 'lng': '113.543050'},
          {'lat': '23.075952', 'lng': '113.540904'}, {'lat': '23.076465', 'lng': '113.530529'},
          {'lat': '23.075399', 'lng': '113.529413'}, {'lat': '23.074609', 'lng': '113.529113'},
          {'lat': '23.074412', 'lng': '113.528340'}, {'lat': '23.074333', 'lng': '113.527353'},
          {'lat': '23.073504', 'lng': '113.526921'}, {'lat': '23.071727', 'lng': '113.526790'},
          {'lat': '23.071490', 'lng': '113.527992'}, {'lat': '23.070227', 'lng': '113.530224'},
          {'lat': '23.068845', 'lng': '113.530379'}, {'lat': '23.051866', 'lng': '113.519908'},
          {'lat': '23.059645', 'lng': '113.508584'}, {'lat': '23.070740', 'lng': '113.499302'},
          {'lat': '23.070740', 'lng': '113.499302'}, {'lat': '23.064189', 'lng': '113.529480'},
          {'lat': '23.064199', 'lng': '113.529147'}, {'lat': '23.063942', 'lng': '113.529212'},
          {'lat': '23.064159', 'lng': '113.529491'}, {'lat': '23.063587', 'lng': '113.528139'},
          {'lat': '23.063449', 'lng': '113.528697'}, {'lat': '23.064013', 'lng': '113.529390'},
          {'lat': '23.063845', 'lng': '113.529766'}, {'lat': '23.063529', 'lng': '113.530452'},
          {'lat': '23.063668', 'lng': '113.531182'}, {'lat': '23.060322', 'lng': '113.525871'},
          {'lat': '23.060309', 'lng': '113.526174'}, {'lat': '23.060783', 'lng': '113.525788'},
          {'lat': '23.060941', 'lng': '113.526217'}, {'lat': '23.059539', 'lng': '113.524651'},
          {'lat': '23.059835', 'lng': '113.527124'}, {'lat': '23.059184', 'lng': '113.528476'},
          {'lat': '23.058789', 'lng': '113.527124'}, {'lat': '23.064177', 'lng': '113.527783'},
          {'lat': '23.064177', 'lng': '113.527783'}, {'lat': '23.062509', 'lng': '113.528051'},
          {'lat': '23.061068', 'lng': '113.527686'}, {'lat': '23.061008', 'lng': '113.525369'},
          {'lat': '23.064082', 'lng': '113.530671'}, {'lat': '23.064243', 'lng': '113.531250'},
          {'lat': '23.064197', 'lng': '113.529447'}, {'lat': '23.063950', 'lng': '113.529651'},
          {'lat': '23.063738', 'lng': '113.530604'}, {'lat': '23.063827', 'lng': '113.531130'},
          {'lat': '23.064103', 'lng': '113.530035'}, {'lat': '23.060963', 'lng': '113.526230'},
          {'lat': '23.061151', 'lng': '113.526970'}, {'lat': '23.061506', 'lng': '113.523086'},
          {'lat': '23.061012', 'lng': '113.523236'}, {'lat': '23.060479', 'lng': '113.524202'},
          {'lat': '23.063824', 'lng': '113.529052'}, {'lat': '23.061441', 'lng': '113.533872'},
          {'lat': '23.061352', 'lng': '113.533568'}, {'lat': '23.061629', 'lng': '113.534112'},
          {'lat': '23.061293', 'lng': '113.533983'}, {'lat': '23.069700', 'lng': '113.522270'},
          {'lat': '23.068743', 'lng': '113.522066'}, {'lat': '23.068920', 'lng': '113.523740'},
          {'lat': '23.069276', 'lng': '113.524276'}, {'lat': '23.069710', 'lng': '113.521487'},
          {'lat': '23.069433', 'lng': '113.520350'}, {'lat': '23.064013', 'lng': '113.529390'},
          {'lat': '23.064358', 'lng': '113.527309'}, {'lat': '23.063951', 'lng': '113.529265'},
          {'lat': '23.061729', 'lng': '113.514535'}, {'lat': '23.061522', 'lng': '113.514481'},
          {'lat': '23.061640', 'lng': '113.514975'}, {'lat': '23.061462', 'lng': '113.515511'},
          {'lat': '23.061403', 'lng': '113.516005'}, {'lat': '23.060416', 'lng': '113.513988'},
          {'lat': '23.060258', 'lng': '113.515125'}, {'lat': '23.055868', 'lng': '113.529615'},
          {'lat': '23.055977', 'lng': '113.529991'}, {'lat': '23.056431', 'lng': '113.529046'},
          {'lat': '23.054891', 'lng': '113.527888'}, {'lat': '23.068979', 'lng': '113.527760'},
          {'lat': '23.068990', 'lng': '113.526891'}, {'lat': '23.069602', 'lng': '113.527406'},
          {'lat': '23.069602', 'lng': '113.527406'}, {'lat': '23.053202', 'lng': '113.526403'},
          {'lat': '23.052758', 'lng': '113.525985'}, {'lat': '23.047633', 'lng': '113.520682'},
          {'lat': '23.047584', 'lng': '113.520865'}, {'lat': '23.047564', 'lng': '113.521295'},
          {'lat': '23.047742', 'lng': '113.520694'}, {'lat': '23.047445', 'lng': '113.521059'},
          {'lat': '23.046596', 'lng': '113.521273'}, {'lat': '23.046932', 'lng': '113.522088'},
          {'lat': '23.046162', 'lng': '113.521659'}, {'lat': '23.044740', 'lng': '113.522432'},
          {'lat': '23.063911', 'lng': '113.527722'}, {'lat': '23.061960', 'lng': '113.523558'},
          {'lat': '23.061921', 'lng': '113.523301'}, {'lat': '23.061911', 'lng': '113.523558'},
          {'lat': '23.063968', 'lng': '113.513362'}, {'lat': '23.063864', 'lng': '113.513639'},
          {'lat': '23.064100', 'lng': '113.513317'}, {'lat': '23.061138', 'lng': '113.510603'},
          {'lat': '23.060931', 'lng': '113.510893'}, {'lat': '23.060753', 'lng': '113.510077'},
          {'lat': '23.060753', 'lng': '113.510077'}, {'lat': '23.059973', 'lng': '113.509508'},
          {'lat': '23.060131', 'lng': '113.510238'}, {'lat': '23.060289', 'lng': '113.508693'},
          {'lat': '23.060802', 'lng': '113.509616'}, {'lat': '23.061197', 'lng': '113.511847'},
          {'lat': '23.104596', 'lng': '113.454760'}, {'lat': '23.059848', 'lng': '113.525729'},
          {'lat': '23.059586', 'lng': '113.525941'}, {'lat': '23.058974', 'lng': '113.523517'},
          {'lat': '23.056763', 'lng': '113.524419'}, {'lat': '23.054216', 'lng': '113.525985'},
          {'lat': '23.064887', 'lng': '113.534950'}, {'lat': '23.064305', 'lng': '113.535068'},
          {'lat': '23.062409', 'lng': '113.534296'}, {'lat': '23.047655', 'lng': '113.520557'}]

    for i in range(200):
        action = {}
        u_id = "0000" + str(i+1)
        timing = str(int(time.time())-random.randint(0,5000))
        action['timestamp'] = timing
        action["u_id"] = u_id[-4:]
        action['lat'] = ll[i]['lat']
        action['lng'] = ll[i]['lng']
        action['gps_timestamp'] = str(int(timing)-2)
        action['speed']= format(random.uniform(0,20), '.2f')
        action['direction'] = random.randint(0,360)
        print(action)
        # send_data(es, action)
        # print("=================发送成功==============")
        # time.sleep(0.2)

def send_data(es, data):
    es.index(index="user_point",doc_type="test",body = data)

if __name__ == '__main__':

    es = Elasticsearch(['Commit-Master:9200'])
    create_data(es)








